home *** CD-ROM | disk | FTP | other *** search
- /*
- File: Camera.c
-
- Contains: My Application Shell.
-
- Written by: John Wang
-
- Copyright: © 1994 by Apple Computer, Inc., all rights reserved.
-
- Change History (most recent first):
-
- <1> 09/30/94 JW New.
-
- To Do:
-
- */
-
- #ifdef THINK_C
- #define applec
- #endif
-
- #include "CMDriver.h"
- #include "GeoPortErrors.h"
-
- #include "QuickTakePICT.h"
- #include "CameraUtils.h"
-
- /* ------------------------------------------------------------------------- */
- /* ------------------------------------------------------------------------- */
- /* ------------------------------------------------------------------------- */
-
- // These routines are for opening the driver, connecting, disconnecting, and closing the driver.
-
- /*
- Description: OpenCameraDriver()
-
- Format Params:
- Name Usage Description/Assumptions
- ---- ---- -----------------------
- camData PO Returned.
-
- Usage: P=Parameter,R=ReturnValue,E=External,G=FileGlobal,L=Local,I=Input,O=Output
-
- Error Handling: If noErr is returned, you can assume that the camera driver was opened properly.
- If err is returned, *camData will be nil.
-
- Special Notes: If this fails, you don't need to call CloseCameraDriver.
-
- */
-
- OSErr OpenCameraDriver(CameraData *camData)
- {
- OSErr err;
-
- // Check input.
- if ( camData == nil )
- return ( paramErr );
-
- // Initialize.
- *camData = nil;
-
- // Open driver.
- err = CmOpenDriver(camData);
- if ( err != noErr ) {
- *camData = nil;
- goto bail;
- }
- if ( *camData == nil ) {
- err = -1;
- goto bail;
- }
-
- // Done!
- return ( noErr );
-
- bail:
- // Error!
- return ( err );
- }
-
- /* ------------------------------------------------------------------------- */
-
- /*
- Description: CloseCameraDriver()
-
- Format Params:
- Name Usage Description/Assumptions
- ---- ---- -----------------------
- camData PI Valid camData from OpenCameraDriver.
-
- Usage: P=Parameter,R=ReturnValue,E=External,G=FileGlobal,L=Local,I=Input,O=Output
-
- Error Handling: If noErr is returned, you can assume that the camera driver was closed properly.
-
- Special Notes: Returns error.
-
- */
-
- OSErr CloseCameraDriver(CameraData camData)
- {
- OSErr err;
-
- // Close driver.
- if ( camData != nil )
- err = CmCloseDriver(camData);
- else
- err = paramErr;
-
- return ( err );
- }
-
- /* ------------------------------------------------------------------------- */
-
- /*
- Description: ConnectCamera()
-
- Format Params:
- Name Usage Description/Assumptions
- ---- ---- -----------------------
- camData PI Valid camData from OpenCameraDriver.
- portInfoHandle PO Returned.
- cameraType PO Returned if not nil. See include file for camera types.
-
- Usage: P=Parameter,R=ReturnValue,E=External,G=FileGlobal,L=Local,I=Input,O=Output
-
- Error Handling: If noErr is returned, you can assume that the camera was connected properly.
- If error occured, *portInfoHandle will be nil.
-
- Special Notes: Does not close camera driver.
-
- */
-
- OSErr ConnectCamera(CameraData camData, CmPortInfoHandle *portInfoHandle, short *cameraType)
- {
- OSErr err;
- unsigned long portIndex;
- Boolean foundCamera;
- short tempCameraType;
-
- // Initialize.
- if ( portInfoHandle == nil || camData == nil ) {
- err = paramErr;
- goto bail;
- }
-
- // Keep looking until either an error occurs, or a camera is found.
- portIndex = 1;
- foundCamera = false;
- while ( (!foundCamera) ) {
- // Get info.
- err = CmGetPortInfo(camData, portIndex, portInfoHandle);
-
- // If an error occured, then assume there are no more ports to check. In this case, we exit.
- if ( err != noErr ) {
- // No more ports to check.
- *portInfoHandle = nil;
- goto bail;
- }
-
- // Double check to make sure the info handle is not nil.
- if ( *portInfoHandle == nil ) {
- err = paramErr;
- goto bail;
- }
- portIndex++;
-
- // If we've gotten this far, then it means we have information for one port. Now, let's see if there is
- // QuickTake camera connected to a free port. If not, then camera is NOT found.
- if ((**(*portInfoHandle)).portStatus == kGeoPortFree ) {
- // Check for camera and Return cameraType if requested.
-
- // Code for QuickTake™ 150 driver.
- #if 1
- if ( (**(*portInfoHandle)).manufacturerID == 0x101 && (**(*portInfoHandle)).productID == 0x100 ) {
- tempCameraType = kVenusCamera;
- foundCamera = true;
- } else if ( (**(*portInfoHandle)).manufacturerID == 0x101 && (**(*portInfoHandle)).productID == 0x101 ) {
- tempCameraType = kVenusCamera;
- foundCamera = true;
- } else if ( (**(*portInfoHandle)).manufacturerID == 0xc8 && (**(*portInfoHandle)).productID == 0x1 ) {
- tempCameraType = kNimbusCamera;
- foundCamera = true;
- } else {
- tempCameraType = kUnknownCamera;
- }
- // Code for OLD QuickTake™ 100 driver.
- #else
- if ( (**(*portInfoHandle)).beaconParam == 0x100 || (**(*portInfoHandle)).beaconParam == 0x101 ) {
- tempCameraType = kVenusCamera;
- foundCamera = true;
- } else if ( (**(*portInfoHandle)).beaconParam == 0x200 ) {
- tempCameraType = kNimbusCamera;
- foundCamera = true;
- } else {
- tempCameraType = kUnknownCamera;
- }
- #endif
- }
-
- // Dispose of the info handle if camera is NOT found. We return the info handle if there camera is found.5
- if ( foundCamera == false ) {
- err = CmDisposePortInfo(camData, *portInfoHandle);
- *portInfoHandle = nil;
- if ( err != noErr )
- goto bail;
- }
- }
-
- // Connect.
- err = CmConnect(camData, *portInfoHandle);
- if ( err != noErr )
- goto bail;
-
- if ( cameraType != nil ) {
- *cameraType = tempCameraType;
- }
-
- return ( noErr );
-
- bail:
- DisconnectCamera(camData, *portInfoHandle);
-
- *portInfoHandle = nil;
-
- return ( err );
- }
-
- /* ------------------------------------------------------------------------- */
-
- /*
- Description: DisconnectCamera()
-
- Format Params:
- Name Usage Description/Assumptions
- ---- ---- -----------------------
- camData PI Valid camData from OpenCameraDriver.
- portInfoHandle PI Valid portInfoHandle from ConnectCamera.
-
- Usage: P=Parameter,R=ReturnValue,E=External,G=FileGlobal,L=Local,I=Input,O=Output
-
- Error Handling: Completes disconnect even if error is returned.
-
- Special Notes: xxx put other comments here xxx
-
- */
-
- OSErr DisconnectCamera(CameraData camData, CmPortInfoHandle portInfoHandle)
- {
- OSErr err1, err2;
-
- if ( camData != nil && portInfoHandle != nil) {
- err1 = CmDisposePortInfo(camData, portInfoHandle);
- }
- if ( camData != nil ) {
- err2 = CmDisconnect(camData);
- }
-
- if ( err1 != noErr )
- return ( err1 );
- return ( err2 );
- }
-
- /* ------------------------------------------------------------------------- */
- /* ------------------------------------------------------------------------- */
- /* ------------------------------------------------------------------------- */
-
- // These routines require you to connect and disconnect to the driver youself.
-
- /*
- Description: GrabOnePicture()
- Given and image number, load a pict, thumbnail,
- thumbnailHeader, and pictureInfo as requested.
-
- Format Params:
- Name Usage Description/Assumptions
- ---- ---- -----------------------
- camData PI Valid camData from OpenCameraDriver.
- imageNumber PI Pass in an image number.
- pict PO If pict is not nil, then it will be returned. The pict is unlocked.
- thumbnail PO thumbnail is not nil, then it will be returned. The thumbnail is unlocked.
- thumbnailHeader PO If thumbnailHeader is not nil then it will be returned.
- pictureInfo PO if pictureInfo is not nil, then it will be returned.
-
- Usage: P=Parameter,R=ReturnValue,E=External,G=FileGlobal,L=Local,I=Input,O=Output
-
- Error Handling: If noErr is returned, you can assume that everything requested has been returned.
-
- Special Notes: xxx put other comments here xxx
-
- */
-
- OSErr GrabOnePicture(CameraData camData, short cameraType, short imageNumber, PicHandle *pict, Handle *thumbnail,
- ThumbnailHeader *thumbnailHeader, CmPictureInfo *pictureInfo)
- {
- OSErr err;
- Handle imageBuffer;
- Handle *imageBufferPtr;
- Handle thumbnailBuffer;
- Handle *thumbnailBufferPtr;
- ImageHeader imageHeader;
- CmPictureInfo tempPictureInfo;
- CmColorMatrix colorMatrix;
- BufferPtr decompTable;
- Boolean customColorMatrix;
-
- // Make some temporary variables.
- decompTable = nil;
- imageBuffer = nil;
- if ( pict != nil ) {
- imageBufferPtr = &imageBuffer;
- } else
- imageBufferPtr = nil;
- thumbnailBuffer = nil;
- if ( thumbnail != nil ) {
- thumbnailBufferPtr = &thumbnailBuffer;
- } else
- thumbnailBufferPtr = nil;
-
- // Get image or thumbnail as requested.
- err = GrabOneImage(camData, imageNumber, imageBufferPtr, thumbnailBufferPtr, &tempPictureInfo);
- if ( err != noErr ) {
- goto bail;
- }
-
- // Make sure data is returned properly.
- if ( pict != nil ) {
- if (imageBuffer == nil ) {
- err = memFullErr;
- goto bail;
- }
- }
- if ( thumbnail != nil ) {
- if (thumbnailBuffer == nil ) {
- err = memFullErr;
- goto bail;
- }
- }
-
- // Build imageHeader no matter what.
- if ( cameraType == kVenusCamera ) {
- imageHeader.signature = 'qktk';
- imageHeader.version = 1;
- } else if ( cameraType == kNimbusCamera ) {
- imageHeader.signature = 'qktn';
- imageHeader.version = 2;
- } else {
- err = paramErr;
- goto bail;
- }
- if ( tempPictureInfo.pictureMode == 16 )
- imageHeader.flags = kQktkHiResImage;
- else if ( tempPictureInfo.pictureMode == 32 )
- imageHeader.flags = kQktkStdResImage;
- else {
- err = paramErr;
- goto bail;
- }
- imageHeader.dataSize = tempPictureInfo.imageDataSize;
-
- customColorMatrix = false;
- if ( pict != nil ) {
- // Get matrix if it is newer and if a pict is requested.
- err = GetCameraMatrix(camData, &colorMatrix);
- if ( err == noErr )
- customColorMatrix = true;
- else if ( err != -1 )
- goto bail;
-
- // Get decompTable if it is newer and if a pict is requested.
- if ( cameraType == kVenusCamera ) {
- decompTable = (BufferPtr) NewPtr(kCmDecompTableSize100);
- } else if ( cameraType == kNimbusCamera ) {
- decompTable = (BufferPtr) NewPtr(kCmDecompTableSize150);
- } else {
- err = paramErr;
- goto bail;
- }
- if ( decompTable == nil ) {
- err = memFullErr;
- goto bail;
- }
- err = GetCameraDecompTable(camData, decompTable);
- if ( err == -1 ) {
- if ( decompTable != nil )
- DisposePtr((Ptr) decompTable);
- decompTable = nil;
- } else if ( err != noErr )
- goto bail;
- }
-
- // Make the pict using imageHeader.
- if ( pict != nil ) {
- CmColorMatrixPtr colorMatrixPtr;
-
- if ( customColorMatrix )
- colorMatrixPtr = &colorMatrix;
- else
- colorMatrixPtr = nil;
- HLock(imageBuffer);
- err = MakeQuickTakePicture(&imageHeader, *imageBuffer, colorMatrixPtr, decompTable, pict);
- HUnlock(imageBuffer);
- if ( err != noErr ) {
- goto bail;
- }
- }
-
- // Make thumbnail.
- if ( thumbnail != nil ) {
- HLock(thumbnailBuffer);
- *thumbnail = thumbnailBuffer;
- HUnlock(thumbnailBuffer);
- }
-
- // Make thumbnail header.
- if ( thumbnailHeader != nil ) {
- thumbnailHeader->signature = imageHeader.signature;
- thumbnailHeader->version = imageHeader.version;
- thumbnailHeader->flags = kQktkThumbnailImage | (imageHeader.flags & kQktkRotationBits);
- thumbnailHeader->dataSize = 2400;
- }
-
- // Fill in pictureInfo.
- if ( pictureInfo != nil ) {
- *pictureInfo = tempPictureInfo;
- }
-
- err = noErr;
-
- bail:
- if ( decompTable != nil )
- DisposePtr((Ptr) decompTable);
-
- // imageBuffer is no longer needed because it is stored into Picture.
- if ( imageBuffer != nil )
- DisposHandle(imageBuffer);
-
- return ( err );
- }
-
- /* ------------------------------------------------------------------------- */
-
- /*
- Description: GrabOneImage()
- Given and image number, load imageBuffer, thumbnailBuffer, and pictureInfo
- as requested.
-
- Format Params:
- Name Usage Description/Assumptions
- ---- ---- -----------------------
- camData PI Valid camData from OpenCameraDriver.
- imageNumber PI Pass in an image number.
- imageBuffer PO If imageBuffer is not nil, then it will be returned.
- thumbnailBuffer PO If thumbnailBuffer is not nil, then it will be returned.
- pictureInfo PO pictureInfo is not nil, then then it will be returned.
-
- Usage: P=Parameter,R=ReturnValue,E=External,G=FileGlobal,L=Local,I=Input,O=Output
-
- Error Handling: If noErr is returned, you can assume that everything requested has been returned.
-
- Special Notes: xxx put other comments here xxx
-
- */
-
- OSErr GrabOneImage(CameraData camData, short imageNumber, Handle *imageBuffer, Handle *thumbnailBuffer,
- CmPictureInfo *pictureInfo)
- {
- OSErr err;
- CmPictureInfo tempPictureInfo;
- Boolean firstRead;
- unsigned long actualBytes;
-
- // Initialize.
- if ( imageBuffer != nil )
- *imageBuffer = nil;
- if ( thumbnailBuffer != nil )
- *thumbnailBuffer = nil;
-
- // Get picture info.
- err = CmGetPictureInfo(camData, imageNumber, &tempPictureInfo);
- if ( err != noErr )
- goto bail;
-
- // If pictureInfo is requested, return it.
- if ( pictureInfo != nil )
- *pictureInfo = tempPictureInfo;
-
- // If imageBuffer is requested, return it.
- if ( imageBuffer != nil ) {
- *imageBuffer = NewHandle(tempPictureInfo.imageDataSize);
- if ( *imageBuffer == nil ) {
- err = memFullErr;
- goto bail;
- }
- MoveHHi(*imageBuffer);
- HLock(*imageBuffer);
- firstRead = true;
- err = CmGetFullSizeImage(camData, imageNumber, (unsigned char *) **imageBuffer, tempPictureInfo.imageDataSize, firstRead, &actualBytes);
- HUnlock(*imageBuffer);
- if ( err != noErr )
- goto bail;
- if ( actualBytes != tempPictureInfo.imageDataSize ) {
- err = ioErr;
- goto bail;
- }
- }
-
- // If thumbnailBuffer is requested, return it.
- if ( thumbnailBuffer != nil ) {
- *thumbnailBuffer = NewHandle(2400);
- if ( *imageBuffer == nil ) {
- err = memFullErr;
- goto bail;
- }
- MoveHHi(*thumbnailBuffer);
- HLock(*thumbnailBuffer);
- firstRead = true;
- err = CmGetThumbnailImage(camData, imageNumber, (unsigned char *) **thumbnailBuffer, 2400, firstRead, &actualBytes);
- HUnlock(*thumbnailBuffer);
- if ( err != noErr )
- goto bail;
- if ( actualBytes != 2400 ) {
- err = ioErr;
- goto bail;
- }
- }
-
- bail:
- return ( err );
- }
-
- /* ------------------------------------------------------------------------- */
-
- /*
- Description: GetCameraInfo()
- Get camera info.
-
- Format Params:
- Name Usage Description/Assumptions
- ---- ---- -----------------------
- camData PI Valid camData from OpenCameraDriver.
- cameraInfo PO Camera info returned.
-
- Usage: P=Parameter,R=ReturnValue,E=External,G=FileGlobal,L=Local,I=Input,O=Output
-
- Error Handling: If noErr is returned, you can assume that everything requested has been returned.
-
- Special Notes: xxx put other comments here xxx
-
- */
-
- OSErr GetCameraInfo(CameraData camData, CmCameraInfo *cameraInfo)
- {
- OSErr err;
-
- // Check input parameters.
- if ( cameraInfo == nil) {
- err = paramErr;
- goto bail;
- }
-
- // Get Camera Info.
- err = CmGetCameraInfo(camData, cameraInfo);
- if ( err != noErr )
- goto bail;
-
- bail:
- return ( err );
- }
-
- /* ------------------------------------------------------------------------- */
-
- /*
- Description: GetCameraMatrix()
- Get camera matrix.
-
- Format Params:
- Name Usage Description/Assumptions
- ---- ---- -----------------------
- camData PI Valid camData from OpenCameraDriver.
- cameraMatrix PO Camera matrix returned. If camera version is 1, then err = -1 is
- returned.
-
- Usage: P=Parameter,R=ReturnValue,E=External,G=FileGlobal,L=Local,I=Input,O=Output
-
- Error Handling: If noErr is returned, you can assume that everything requested has been returned.
-
- Special Notes: xxx put other comments here xxx
-
- */
-
- OSErr GetCameraMatrix(CameraData camData, CmColorMatrixPtr colorMatrix)
- {
- OSErr err;
- CmCameraInfo cameraInfo;
-
- // Check input parameters.
- if ( colorMatrix == nil) {
- err = paramErr;
- goto bail;
- }
-
- // Get Camera Info.
- err = CmGetCameraInfo(camData, &cameraInfo);
- if ( err != noErr )
- goto bail;
-
- // If version is not 1, then return color matrix, otherwise, return err = -1.
- if ( cameraInfo.colorMatrixVersion[0] != 0 || cameraInfo.colorMatrixVersion[1] != 1 ) {
- // Get Camera Info.
- err = CmGetColorCorrectionMatrix(camData, colorMatrix);
- if ( err != noErr )
- goto bail;
- } else
- err = -1;
-
- bail:
- return ( err );
- }
-
- /* ------------------------------------------------------------------------- */
-
- /*
- Description: GetCameraDecompTable()
- Get camera decompression table.
-
- Format Params:
- Name Usage Description/Assumptions
- ---- ---- -----------------------
- camData PI Valid camData from OpenCameraDriver.
- decompTable PO Camera decomp table returned. If camera version is 1, then
- err = -1 is returned.
-
- Usage: P=Parameter,R=ReturnValue,E=External,G=FileGlobal,L=Local,I=Input,O=Output
-
- Error Handling: If noErr is returned, you can assume that everything requested has been returned.
-
- Special Notes: xxx put other comments here xxx
-
- */
-
- OSErr GetCameraDecompTable(CameraData camData, BufferPtr decompTable)
- {
- OSErr err;
- CmCameraInfo cameraInfo;
-
- // Check input parameters.
- if ( decompTable == nil) {
- err = paramErr;
- goto bail;
- }
-
- // Get Camera Info.
- err = CmGetCameraInfo(camData, &cameraInfo);
- if ( err != noErr )
- goto bail;
-
- // If version is not 1, then return color matrix, otherwise, return err = -1.
- if ( cameraInfo.compDecompParamVersion[0] != 0 || cameraInfo.compDecompParamVersion[1] != 1 ) {
- // Get Camera Info.
- err = CmGetDecompTable(camData, decompTable);
- if ( err != noErr )
- goto bail;
- } else
- err = -1;
-
- bail:
- return ( err );
- }
-
- /* ------------------------------------------------------------------------- */
- /* ------------------------------------------------------------------------- */
- /* ------------------------------------------------------------------------- */
-
- // These routines are complete. You don't have connect or disconnect to the camera. These
- // routines are useful if you call it only once. However, if you plan to call it multiple times,
- // then it is more efficient to connect and disconnect yourself only once.
-
- /*
- Description: GrabOnePictureComplete()
- Given and image number, load a pict, thumbnail,
- thumbnailHeader, and pictureInfo as requested.
-
- Format Params:
- Name Usage Description/Assumptions
- ---- ---- -----------------------
- imageNumber PI Pass in an image number.
- pict PO If pict is not nil, then it will be returned. The pict is unlocked.
- thumbnail PO thumbnail is not nil, then it will be returned. The thumbnail is unlocked.
- thumbnailHeader PO If thumbnailHeader is not nil then it will be returned.
- pictureInfo PO if pictureInfo is not nil, then it will be returned.
-
- Usage: P=Parameter,R=ReturnValue,E=External,G=FileGlobal,L=Local,I=Input,O=Output
-
- Error Handling: If noErr is returned, you can assume that everything requested has been returned.
-
- Special Notes: xxx put other comments here xxx
-
- */
-
- OSErr GrabOnePictureComplete(short imageNumber, PicHandle *pict, Handle *thumbnail,
- ThumbnailHeader *thumbnailHeader, CmPictureInfo *pictureInfo)
- {
- OSErr err;
- CameraData camData;
- CmPortInfoHandle portInfoHandle;
-
- Handle imageBuffer;
- Handle *imageBufferPtr;
- Handle thumbnailBuffer;
- Handle *thumbnailBufferPtr;
- short cameraType;
- ImageHeader imageHeader;
- CmPictureInfo tempPictureInfo;
- CmColorMatrix colorMatrix;
- BufferPtr decompTable;
- Boolean customColorMatrix;
-
- // Open camera driver. If can't open camera, then return immediately.
- err = OpenCameraDriver(&camData);
- if ( err != noErr )
- return ( err );
-
- // Make some temporary variables.
- imageBuffer = nil;
- if ( pict != nil ) {
- imageBufferPtr = &imageBuffer;
- } else
- imageBufferPtr = nil;
- thumbnailBuffer = nil;
- if ( thumbnail != nil ) {
- thumbnailBufferPtr = &thumbnailBuffer;
- } else
- thumbnailBufferPtr = nil;
-
- // Connect to camera.
- err = ConnectCamera(camData, &portInfoHandle, &cameraType);
- if ( err != noErr )
- goto bail;
-
- // Get image or thumbnail as requested.
- err = GrabOneImage(camData, imageNumber, imageBufferPtr, thumbnailBufferPtr, &tempPictureInfo);
- if ( err != noErr )
- goto bail;
-
- // Make sure data is returned properly.
- if ( pict != nil ) {
- if (imageBuffer == nil ) {
- err = memFullErr;
- goto bail;
- }
- }
- if ( thumbnail != nil ) {
- if (thumbnailBuffer == nil ) {
- err = memFullErr;
- goto bail;
- }
- }
-
- // Build imageHeader no matter what.
- if ( cameraType == kVenusCamera ) {
- imageHeader.signature = 'qktk';
- imageHeader.version = 1;
- } else if ( cameraType == kNimbusCamera ) {
- imageHeader.signature = 'qktn';
- imageHeader.version = 2;
- } else {
- err = paramErr;
- goto bail;
- }
- if ( tempPictureInfo.pictureMode == 16 )
- imageHeader.flags = kQktkHiResImage;
- else if ( tempPictureInfo.pictureMode == 32 )
- imageHeader.flags = kQktkStdResImage;
- else {
- err = paramErr;
- goto bail;
- }
- imageHeader.dataSize = tempPictureInfo.imageDataSize;
-
- customColorMatrix = false;
- if ( pict != nil ) {
- // Get matrix if it is newer and if a pict is requested.
- err = GetCameraMatrix(camData, &colorMatrix);
- if ( err == noErr )
- customColorMatrix = true;
- else if ( err != -1 )
- goto bail;
-
- // Get decompTable if it is newer and if a pict is requested.
- if ( cameraType == kVenusCamera ) {
- decompTable = (BufferPtr) NewPtr(kCmDecompTableSize100);
- } else if ( cameraType == kNimbusCamera ) {
- decompTable = (BufferPtr) NewPtr(kCmDecompTableSize150);
- } else {
- err = paramErr;
- goto bail;
- }
- if ( decompTable == nil ) {
- err = memFullErr;
- goto bail;
- }
- err = GetCameraDecompTable(camData, decompTable);
- if ( err == -1 ) {
- if ( decompTable != nil )
- DisposePtr((Ptr) decompTable);
- decompTable = nil;
- } else if ( err != noErr )
- goto bail;
- }
-
- // Make the pict using imageHeader.
- if ( pict != nil ) {
- CmColorMatrixPtr colorMatrixPtr;
-
- if ( customColorMatrix )
- colorMatrixPtr = &colorMatrix;
- else
- colorMatrixPtr = nil;
- HLock(imageBuffer);
- // MakeQuickTakePicture returns an unlocked pict.
- err = MakeQuickTakePicture(&imageHeader, *imageBuffer, colorMatrixPtr, decompTable, pict);
- HUnlock(imageBuffer);
- if ( err != noErr )
- goto bail;
- }
-
- // Make thumbnail.
- if ( thumbnail != nil ) {
- HLock(thumbnailBuffer);
- *thumbnail = thumbnailBuffer;
- HUnlock(thumbnailBuffer);
- }
-
- // Make thumbnail header.
- if ( thumbnailHeader != nil ) {
- thumbnailHeader->signature = imageHeader.signature;
- thumbnailHeader->version = imageHeader.version;
- thumbnailHeader->flags = kQktkThumbnailImage | (imageHeader.flags & kQktkRotationBits);
- thumbnailHeader->dataSize = 2400;
- }
-
- // Fill in pictureInfo.
- if ( pictureInfo != nil ) {
- *pictureInfo = tempPictureInfo;
- }
-
- err = noErr;
-
- bail:
- // imageBuffer is no longer needed because it is stored into Picture.
- if ( imageBuffer != nil )
- DisposHandle(imageBuffer);
-
- // If we failed at any point, remember to close camera. Otherwise, it won't open up the next time.
- DisconnectCamera(camData, portInfoHandle);
- CloseCameraDriver(camData);
-
- return ( err );
- }
-
- /* ------------------------------------------------------------------------- */
-
- /*
- Description: GrabOneImageComplete()
- Given and image number, load imageBuffer, thumbnailBuffer, pictureInfo, and
- cameraType as requested.
-
- Format Params:
- Name Usage Description/Assumptions
- ---- ---- -----------------------
- imageNumber PI Pass in an image number.
- imageBuffer PO If imageBuffer is not nil, then it will be returned.
- thumbnailBuffer PO If thumbnailBuffer is not nil, then it will be returned.
- pictureInfo PO pictureInfo is not nil, then then it will be returned.
- cameraType PO If cameraType is not nil then it will be returned.
-
- Usage: P=Parameter,R=ReturnValue,E=External,G=FileGlobal,L=Local,I=Input,O=Output
-
- Error Handling: If noErr is returned, you can assume that everything requested has been returned.
-
- Special Notes: xxx put other comments here xxx
-
- */
-
- OSErr GrabOneImageComplete(short imageNumber, Handle *imageBuffer, Handle *thumbnailBuffer,
- CmPictureInfo *pictureInfo, short *cameraType)
- {
- OSErr err;
- CameraData camData;
- CmPortInfoHandle portInfoHandle;
-
- CmPictureInfo tempPictureInfo;
- Boolean firstRead;
- unsigned long actualBytes;
-
- // Open camera driver. If can't open camera, then return immediately.
- err = OpenCameraDriver(&camData);
- if ( err != noErr )
- return ( err );
-
- // Initialize.
- if ( imageBuffer != nil )
- *imageBuffer = nil;
- if ( thumbnailBuffer != nil )
- *thumbnailBuffer = nil;
- if ( cameraType != nil )
- *cameraType = kUnknownCamera;
-
- // Connect to camera.
- err = ConnectCamera(camData, &portInfoHandle, cameraType);
- if ( err != noErr )
- goto bail;
-
- // Get picture info.
- err = CmGetPictureInfo(camData, imageNumber, &tempPictureInfo);
- if ( err != noErr )
- goto bail;
-
- // If pictureInfo is requested, return it.
- if ( pictureInfo != nil )
- *pictureInfo = tempPictureInfo;
-
- // If imageBuffer is requested, return it.
- if ( imageBuffer != nil ) {
- *imageBuffer = NewHandle(tempPictureInfo.imageDataSize);
- if ( *imageBuffer == nil ) {
- err = memFullErr;
- goto bail;
- }
- MoveHHi(*imageBuffer);
- HLock(*imageBuffer);
- firstRead = true;
- err = CmGetFullSizeImage(camData, imageNumber, (unsigned char *) **imageBuffer, tempPictureInfo.imageDataSize, firstRead, &actualBytes);
- HUnlock(*imageBuffer);
- if ( err != noErr )
- goto bail;
- if ( actualBytes != tempPictureInfo.imageDataSize ) {
- err = ioErr;
- goto bail;
- }
- }
-
- // If thumbnailBuffer is requested, return it.
- if ( thumbnailBuffer != nil ) {
- *thumbnailBuffer = NewHandle(2400);
- if ( *imageBuffer == nil ) {
- err = memFullErr;
- goto bail;
- }
- MoveHHi(*thumbnailBuffer);
- HLock(*thumbnailBuffer);
- firstRead = true;
- err = CmGetThumbnailImage(camData, imageNumber, (unsigned char *) **thumbnailBuffer, 2400, firstRead, &actualBytes);
- HUnlock(*thumbnailBuffer);
- if ( err != noErr )
- goto bail;
- if ( actualBytes != 2400 ) {
- err = ioErr;
- goto bail;
- }
- }
-
- bail:
- // If we failed at any point, remember to close camera. Otherwise, it won't open up the next time.
- DisconnectCamera(camData, portInfoHandle);
- CloseCameraDriver(camData);
- return ( err );
- }
-
- /* ------------------------------------------------------------------------- */
-
- /*
- Description: GetCameraInfoComplete()
- Get camera info.
-
- Format Params:
- Name Usage Description/Assumptions
- ---- ---- -----------------------
- cameraInfo PO Camera info returned.
-
- Usage: P=Parameter,R=ReturnValue,E=External,G=FileGlobal,L=Local,I=Input,O=Output
-
- Error Handling: If noErr is returned, you can assume that everything requested has been returned.
-
- Special Notes: xxx put other comments here xxx
-
- */
-
- OSErr GetCameraInfoComplete(CmCameraInfo *cameraInfo)
- {
- OSErr err;
- CameraData camData;
- CmPortInfoHandle portInfoHandle;
-
- // Check input parameters.
- if ( cameraInfo == nil) {
- err = paramErr;
- goto bail;
- }
-
- // Open camera driver. If can't open camera, then return immediately.
- err = OpenCameraDriver(&camData);
- if ( err != noErr )
- return ( err );
-
- // Connect to camera.
- err = ConnectCamera(camData, &portInfoHandle, nil);
- if ( err != noErr )
- goto bail;
-
- // Get Camera Info.
- err = CmGetCameraInfo(camData, cameraInfo);
- if ( err != noErr )
- goto bail;
-
- bail:
- // If we failed at any point, remember to close camera. Otherwise, it won't open up the next time.
- DisconnectCamera(camData, portInfoHandle);
- CloseCameraDriver(camData);
-
- return ( err );
- }
-
- /* ------------------------------------------------------------------------- */
-
- /*
- Description: GetCameraMatrixComplete()
- Get camera matrix.
-
- Format Params:
- Name Usage Description/Assumptions
- ---- ---- -----------------------
- cameraMatrix PO Camera matrix returned. If camera version is 1, then err = -1 is
- returned.
-
- Usage: P=Parameter,R=ReturnValue,E=External,G=FileGlobal,L=Local,I=Input,O=Output
-
- Error Handling: If noErr is returned, you can assume that everything requested has been returned.
-
- Special Notes: xxx put other comments here xxx
-
- */
-
- OSErr GetCameraMatrixComplete(CmColorMatrixPtr colorMatrix)
- {
- OSErr err;
- CameraData camData;
- CmPortInfoHandle portInfoHandle;
- CmCameraInfo cameraInfo;
-
- // Check input parameters.
- if ( colorMatrix == nil) {
- err = paramErr;
- goto bail;
- }
-
- // Open camera driver. If can't open camera, then return immediately.
- err = OpenCameraDriver(&camData);
- if ( err != noErr )
- return ( err );
-
- // Connect to camera.
- err = ConnectCamera(camData, &portInfoHandle, nil);
- if ( err != noErr )
- goto bail;
-
- // Get Camera Info.
- err = CmGetCameraInfo(camData, &cameraInfo);
- if ( err != noErr )
- goto bail;
-
- // If version is not 1, then return color matrix, otherwise, return err = -1.
- if ( cameraInfo.colorMatrixVersion[0] != 0 || cameraInfo.colorMatrixVersion[1] != 1 ) {
- // Get Camera Info.
- err = CmGetColorCorrectionMatrix(camData, colorMatrix);
- if ( err != noErr )
- goto bail;
- } else
- err = -1;
-
- bail:
- // If we failed at any point, remember to close camera. Otherwise, it won't open up the next time.
- DisconnectCamera(camData, portInfoHandle);
- CloseCameraDriver(camData);
-
- return ( err );
- }
-
- /* ------------------------------------------------------------------------- */
-
- /*
- Description: GetCameraDecompTableComplete()
- Get camera decompression table.
-
- Format Params:
- Name Usage Description/Assumptions
- ---- ---- -----------------------
- decompTable PO Camera decomp table returned. If camera version is 1, then
- err = -1 is returned.
-
- Usage: P=Parameter,R=ReturnValue,E=External,G=FileGlobal,L=Local,I=Input,O=Output
-
- Error Handling: If noErr is returned, you can assume that everything requested has been returned.
-
- Special Notes: xxx put other comments here xxx
-
- */
-
- OSErr GetCameraDecompTableComplete(BufferPtr decompTable)
- {
- OSErr err;
- CameraData camData;
- CmPortInfoHandle portInfoHandle;
- CmCameraInfo cameraInfo;
-
- // Check input parameters.
- if ( decompTable == nil) {
- err = paramErr;
- goto bail;
- }
-
- // Open camera driver. If can't open camera, then return immediately.
- err = OpenCameraDriver(&camData);
- if ( err != noErr )
- return ( err );
-
- // Connect to camera.
- err = ConnectCamera(camData, &portInfoHandle, nil);
- if ( err != noErr )
- goto bail;
-
- // Get Camera Info.
- err = CmGetCameraInfo(camData, &cameraInfo);
- if ( err != noErr )
- goto bail;
-
- // If version is not 1, then return color matrix, otherwise, return err = -1.
- if ( cameraInfo.compDecompParamVersion[0] != 0 || cameraInfo.compDecompParamVersion[1] != 1 ) {
- // Get Camera Info.
- err = CmGetDecompTable(camData, decompTable);
- if ( err != noErr )
- goto bail;
- } else
- err = -1;
-
- bail:
- // If we failed at any point, remember to close camera. Otherwise, it won't open up the next time.
- DisconnectCamera(camData, portInfoHandle);
- CloseCameraDriver(camData);
-
- return ( err );
- }
-
- /* ------------------------------------------------------------------------- */
- /* ------------------------------------------------------------------------- */
- /* ------------------------------------------------------------------------- */
-
- /*
- Description: GetPictureFrame()
- Get correct picture frame size. This is necessary because
- some pictures may be 320x240 with 144 dpi. This routine will
- give you the recommended picture frame, or the 'best' picture frame.
-
- Format Params:
- Name Usage Description/Assumptions
- ---- ---- -----------------------
- pict PI Must be valid picture.
- exeryPixel PI If true, then return 'best'. Otherwise, use normal picFrame.
- myRect PO Returned rect.
-
- Usage: P=Parameter,R=ReturnValue,E=External,G=FileGlobal,L=Local,I=Input,O=Output
-
- Error Handling: If noErr is returned, you can assume that everything requested has been returned.
-
- Special Notes: xxx put other comments here xxx
-
- */
-
- typedef struct {
- short picSize;
- Rect picFrame;
- short picVersionOp;
- short picVersion;
- short picHeaderOp;
- short picHeaderVersion;
- short picReserved1;
- Fixed picHRes;
- Fixed picVRes;
- Rect picSrcRect;
- long picReserved2;
- } ColorPicture;
-
- void GetPictureFrame(PicHandle pict, Boolean everyPixel, Rect *myRect)
- {
- if ( pict != nil && myRect != nil ) {
- if ( everyPixel ) {
- ColorPicture *newPict;
-
- newPict = (ColorPicture *) *pict;
- if ( newPict->picVersion > 0x200 && newPict->picHeaderOp == 0x0c00 &&
- newPict->picHeaderVersion == -2 )
- *myRect = newPict->picSrcRect;
- else
- *myRect = newPict->picFrame;
- } else {
- *myRect = (**pict).picFrame;
- }
- }
- }
-